//
//  GiftInfoDataSource.m
//  tcpoctest
//
//  Created by gump on 5/27/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "GiftInfoDataSource.h"
#import "GiftInfoDataInfo.h"
#import "LocalPathDataSource.h"
#import "giftInfoImageDataBase.h"

#import "sendGiftInfo.h"

@implementation GiftInfoDataSource

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        GIDic = [[NSMutableDictionary alloc] init];
        GIShowArr = [[NSMutableArray alloc] init];
        GIImageDic = [[NSMutableDictionary alloc] init];
        GIMusicDic = [[NSMutableDictionary alloc] init];
        
        GIDownLoadImageSet = [[NSMutableSet alloc] init];
        
        iGiftTotalCount = 0;
        
        self.strSenderMail = @"";
        
        self.strDBUrl = @"";
        self.strDBID = @"";
        
        pthread_mutex_init(&mutexGI, 0);
        pthread_mutex_init(&mutexGIDownImage, 0);
        pthread_mutex_init(&mutexGIMusic, 0);
        pthread_mutex_init(&mutexSendBack, 0);
    }
    
    return self;
}

-(void)delAllGIDic
{
    pthread_mutex_lock(&mutexGI);
    
    [GIDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGI);
}

-(void)AddGIDicInfo:(GiftInfoDataInfo *)ginfo withGiftID:(NSString *)giftid
{
    pthread_mutex_lock(&mutexGI);
    
    [GIDic setObject:ginfo forKey:giftid];
    
    pthread_mutex_unlock(&mutexGI);
}

-(int)GetDicCount
{
    int icount = 0;
    
    pthread_mutex_lock(&mutexGI);
    
    icount = [GIDic count];
    
    pthread_mutex_unlock(&mutexGI);
    
    return icount;

}

-(int)GetTotalCount
{
    int icount = 0;
    
    pthread_mutex_lock(&mutexGI);
    
    icount = iGiftTotalCount;
    
    pthread_mutex_unlock(&mutexGI);
    
    return icount;
}

-(void)SetShowArrByListID:(int)iid
{
    pthread_mutex_lock(&mutexGI);
    
    [GIShowArr removeAllObjects];
    iGiftTotalCount = 0;
    
    NSArray *infoarr = [GIDic allValues];
    for (GiftInfoDataInfo *ginfo in infoarr) {
        if (ginfo.giftBelongID == iid)
        {
            if(ginfo.giftstate == GIDIS_USE) {
            [GIShowArr addObject:ginfo.strgiftid];
            }
            
            ++iGiftTotalCount;
        }
    }
    
    pthread_mutex_unlock(&mutexGI);
}

-(int)GetShowCount
{
    int icount = 0;
    
    pthread_mutex_lock(&mutexGI);
    
    icount = GIShowArr.count;
    
    pthread_mutex_unlock(&mutexGI);
    
    return icount;
}

-(void)AddToShowArr:(NSString *)giftid
{
    pthread_mutex_lock(&mutexGI);
    
    if (![GIShowArr containsObject:giftid]) {
        
        [GIShowArr addObject:giftid];
        
    }
    
    
    pthread_mutex_unlock(&mutexGI);

}

-(void)DelShowArrByID:(NSString *)iid
{
    pthread_mutex_lock(&mutexGI);
    
    if ([GIShowArr containsObject:iid]) {
        
        [GIShowArr removeObject:iid];
        
    }
    
    
    pthread_mutex_unlock(&mutexGI);
}

-(NSMutableArray *)GetShowArr
{
    NSMutableArray *arr = nil;
    
   // pthread_mutex_lock(&mutexGI);
    
    [GIShowArr sortUsingFunction:customSortGI context:nil];
    
    arr = GIShowArr;
    
   // pthread_mutex_unlock(&mutexGI);
    
    return arr;
}

-(GiftInfoDataInfo *)getGiftInfoDirectByID:(NSString *)iid
{
    return [GIDic objectForKey:iid];
}

NSInteger customSortGI(id obj1, id obj2,void* context)
{
    NSString* obj_1 = (NSString*)obj1;
    NSString* obj_2 = (NSString*)obj2;
    
    NSComparisonResult res = (NSComparisonResult)NSOrderedSame;
    
    do {
        
        GiftInfoDataInfo *ginfo1 = [[DataControl shareControl].giftInfoDS getGiftInfoByID:obj_1];;
        GiftInfoDataInfo *ginfo2 = [[DataControl shareControl].giftInfoDS getGiftInfoByID:obj_2];
        
        if (ginfo1 == nil || ginfo1 == nil) {
            break;
        }
        
        if (ginfo1.giftprice > ginfo2.giftprice) {
            res = (NSComparisonResult)NSOrderedDescending;
        }
        else if(ginfo1.giftprice < ginfo2.giftprice)
        {
            res = (NSComparisonResult)NSOrderedAscending;
        }
        
    } while (false);
    
    return res;
}


-(GiftInfoDataInfo *)getGiftInfoByID:(NSString *)iid
{
    GiftInfoDataInfo *ginfo = nil;
    
    pthread_mutex_lock(&mutexGI);
    
    ginfo = [GIDic objectForKey:iid];
    
    if (ginfo == nil) {
        [[netManager sharedNetManager].sendgiftI sendGiftGetInfo:iid];
    }
    
    pthread_mutex_unlock(&mutexGI);
    
    return ginfo;
}

-(NSString *)searchGiftImagePath:(NSString *)iid
{
    NSString *strpath = @"";
    
    pthread_mutex_lock(&mutexGIDownImage);
    
    strpath = [GIImageDic objectForKey:iid];
    
    pthread_mutex_unlock(&mutexGIDownImage);
    
    if (strpath == nil) {
        strpath = @"";
        
        GiftInfoDataInfo *ginfo = [self getGiftInfoByID:iid];
        
        if (ginfo != nil) {
            
            pthread_mutex_lock(&mutexGIDownImage);
            
            if (ginfo.strgifturl != nil &&
                ![ginfo.strgifturl isEqualToString:@""] ) {
                
                
                    
                    NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ginfo.strgifturl]];
                    
                    if (imagedata != nil && [imagedata length] > 0) {
                        
                        NSString *urlpath = @"";
                        NSString *strname = getFileNameWithDate(FT_GIFT);
                        
                        strname = [NSString stringWithFormat:@"%@%@"
                                   ,strname
                                   ,ginfo.strgiftid];
                        
                        urlpath =[[[DataControl shareControl].localpathds giftPath] stringByAppendingPathComponent:strname];
                        
                        BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
                        
                        do {
                            
                            if (!bsuccess) {
                                break;
                            }
                            
                            self.strDBUrl = urlpath;
                            self.strDBID = iid;
                            
                            [[DataControl shareControl].LCDS addLogicCommand:LC_DB_GIFT_INIMAGEPATH];
                            
                            
                            
                            NSString *stroldpath = [GIImageDic objectForKey:iid];
                            if (stroldpath != nil && ![stroldpath isEqualToString:@""]) {
                                [[FileManager shareControl] deleteFile:stroldpath];
                            }
                            
                            [GIImageDic setObject:urlpath forKey:iid];
                            
                        } while (false);
                    }
                
                [[DataControl shareControl].UICommendDS addUICommandTryLock:UC_REFRESHCURVIEW];
            }
            
                pthread_mutex_unlock(&mutexGIDownImage);
            
        }
    }
    
    return strpath;

}

-(void)DownLoadImage
{
    pthread_mutex_lock(&mutexGIDownImage);
    
    for (NSString *strid in GIDownLoadImageSet) {
        GiftInfoDataInfo *ginfo = [GIDic objectForKey:strid];
        
        if (ginfo == nil) {
            continue;
        }
        
        NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ginfo.strgifturl]];
        
        if (imagedata != nil && [imagedata length] > 0) {
            
            NSString *urlpath = @"";
            NSString *strname = getFileNameWithDate(FT_GIFT);
            
            strname = [NSString stringWithFormat:@"%@%@",strname,ginfo.strgiftid];
            
            urlpath =[[[DataControl shareControl].localpathds giftPath] stringByAppendingPathComponent:strname];
            
            BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
            
            do {
                
                if (!bsuccess) {
                    break;
                }
                
                [[DataBaseCtrol shareControl].giftinfoImageDB insertPath:urlpath withID:strid];
                
                NSString *stroldpath = [GIImageDic objectForKey:strid];
                if (stroldpath != nil && ![stroldpath isEqualToString:@""]) {
                    [[FileManager shareControl] deleteFile:stroldpath];
                }
                
                [GIImageDic setObject:urlpath forKey:strid];
                
            } while (false);
        }
        
    }

    [GIDownLoadImageSet removeAllObjects];
    
    pthread_mutex_unlock(&mutexGIDownImage);

    [[DataControl shareControl].UICommendDS addUICommandTryLock:UC_REFRESHCURVIEW];

    
}

-(void)delAllImageDic
{
    pthread_mutex_lock(&mutexGI);
    
    [GIImageDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGI);
}

-(void)AddPathToImageDic:(NSString *)strpath withID:(NSString *)strid
{
    pthread_mutex_lock(&mutexGI);
    
    NSString *oldpaht = [GIImageDic objectForKey:strid];
    if (oldpaht != nil && ![oldpaht isEqualToString:@""]) {
        [[FileManager shareControl] deleteFile:oldpaht];
    }
    
    [GIImageDic setObject:strpath forKey:strid];
    
    pthread_mutex_unlock(&mutexGI);
}

-(void)AddMusicPath:(NSString *)strpath withID:(NSString *)strid
{
    pthread_mutex_lock(&mutexGIMusic);
    
    NSString *oldpaht = [GIMusicDic objectForKey:strid];
    if (oldpaht != nil && ![oldpaht isEqualToString:@""]) {
        [[FileManager shareControl] deleteFile:oldpaht];
    }
    
    [GIMusicDic setObject:strpath forKey:strid];
    
    pthread_mutex_unlock(&mutexGIMusic);
}

-(NSString *)GetMusicPathByID:(NSString *)strid
{
    NSString *strpath = @"";
    
    pthread_mutex_lock(&mutexGIMusic);
    
    strpath = [GIMusicDic objectForKey:strid];
    if (strpath == nil) {
        strpath = @"";
        
        GiftInfoDataInfo *ginfo = [GIDic objectForKey:strid];
        [self DownLoadMusic:ginfo];
    }
    
    pthread_mutex_unlock(&mutexGIMusic);
    
    return strpath;
}

-(void)DelAllMusicDic
{
    pthread_mutex_lock(&mutexGIMusic);
    
    [GIMusicDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexGIMusic);
}

-(void)DownLoadMusic:(GiftInfoDataInfo *)ginfo
{
    if (ginfo != nil) {
            
            NSData *musicdata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ginfo.giftMusicUrl]];
            if (musicdata != nil) {
                
                NSString *urlpath = @"";
                NSString *strname = getFileNameWithDate(FT_HEADIMAGE);
                
                strname = [NSString stringWithFormat:@"%@music%@",strname,ginfo.strgiftname];
                
                urlpath =[[[DataControl shareControl].localpathds giftPath] stringByAppendingPathComponent:strname];
                
                BOOL bsuccess = [musicdata writeToFile:urlpath atomically:YES];
                
                if (bsuccess) {
                   
                        
                        [[DataControl shareControl].giftInfoDS AddMusicPath:urlpath
                                                                     withID:ginfo.strgiftid];
                    
                    self.strDBMusicUrl = urlpath;
                    self.strDBMusicID = ginfo.strgiftid;
                        [[DataControl shareControl].LCDS addLogicCommand:LC_DB_GIFT_INMUSICPATH];
                        
                    
                }
            }
    }

}

-(void)setSendBackState:(int)istate
{
    pthread_mutex_lock(&mutexSendBack);
    
    iGiftSendBackState = istate;
    
    pthread_mutex_unlock(&mutexSendBack);
}

-(int)getSendBackState
{
    int istate = GSBS_SUCCESS;
    
    pthread_mutex_lock(&mutexSendBack);
    
    istate = iGiftSendBackState;
    
    pthread_mutex_unlock(&mutexSendBack);
    
    return istate;
}

@end
